﻿ using Microsoft.AspNetCore.Mvc;
using Logistics.Operators;

namespace Logistics.Web.Controllers
{
    public class OperatorController : Controller
    {
        private IOperatorsAppService _OperAppService;
        public OperatorController(IOperatorsAppService OperAppService)
        {
            _OperAppService = OperAppService;
        }
        /// <summary>
        /// 显示页面
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 显示方法
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public object Indexme(int page = 1, int limit = 3, string numberid = "", string name = "")
        {

            var getFoodListDto = new GetOperatorsListDto
            {
                Numberid = numberid,
                Name = name,
                SkipCount = (page - 1) * limit,
                MaxResultCount = limit,
                Sorting = "Id asc"
            };

            var foods = _OperAppService.GetOperatorAsync(getFoodListDto).Result;
            var count = foods.TotalCount;
            return Ok(new { data = foods.Items, count = count, code = "0", msg = "" });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="pageIndex"></param>
        public int DeleteIdas(int id)
        {
            var result = _OperAppService.DeleteAsync(id);
            return result.Id;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        //[HttpDelete]
        public int Pdelete(string ids)
        {
            var i = _OperAppService.DeletePdeleteAsync(ids);
            return i;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public IActionResult AddEf(CreateOperatorsDto input)
        {
            var list = _OperAppService.CreateAddAsync(input);
            if (list != null)
            {
                return Content("<script>location.href='/Operator/Index';</script>", "text/html", System.Text.Encoding.UTF8);
            }
            return Ok(list);
        }

        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <returns></returns>
        public IActionResult FanTian(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 反填方法
        /// </summary>
        /// <returns></returns>
        public JsonResult GetBack(int id)
        {
            var result = _OperAppService.GetAsync(id).Result;
            return Json(result);
        }
        /// <summary>
        /// 员工下拉框
        /// </summary>
        /// <returns></returns>
        public JsonResult GetBasic()
        {
            var list = _OperAppService.GetYuanGongs();
            return Json(list);
        }
        /// <summary>
        /// 角色下拉框
        /// </summary>
        /// <returns></returns>
        public JsonResult GetRoleslx()
        {
            var list = _OperAppService.GetRoleslx();
            return Json(list);
        }
        public IActionResult Role(int id)
        {
            @ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 获取所有权限信息，用于绑定树形控件
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllPower()
        {
            var power = _OperAppService.GetJurisdictions();
            return Json(power);
        }

        public JsonResult PostRolePower(int id, string ids)
        {
            int i = _OperAppService.PostRolePower(id, ids);
            return Json(i);
        }

        /// <summary>
        /// 获取角色权限
        /// </summary>
        /// <returns></returns>
        public JsonResult GetRolePower(int id)
        {
            var list = _OperAppService.GetRolePower(id);
            return Json(list);
        }
    }

}
